from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage
from django.shortcuts import render
from django.http.response import HttpResponse
# Create your views here.
from pyecharts import options as opts
from pyecharts.charts import Pie
from pyecharts.charts import Bar
from pyecharts.faker import Faker

from app01.views import transfer_the_page_range
from projectManage.models import Project


def index(request):
    # TODO 先屏蔽，提升页面性能
    c = (
        Pie(init_opts=opts.InitOpts(width='420px',height='260px'))
            .add(
            "",
            [list(z) for z in zip(['项目A', '项目B', '项目C', '项目D', '项目E'], [20000, 19000, 18000, 9800, 9000])],
            center=["45%", "50%"],
        )
            .set_global_opts(
            title_opts=opts.TitleOpts(title="分布"),
            legend_opts=opts.LegendOpts(pos_left="15%"),
        )
            .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
            .render("./caseReportManage/templates/caseReportManage/e_chart_1.html")
    )
    c = (
        Bar(init_opts=opts.InitOpts(width='420px',height='260px'))
            .add_xaxis(["用户A", "用户B", "用户C", "用户D", "用户E", "用户F", "用户G", "用户H"])
            .add_yaxis("用例数", [1500, 1393, 1321, 1223, 1200, 998, 786, 643], category_gap=0, color=Faker.rand_color())
            .set_global_opts(title_opts=opts.TitleOpts(title="用户生成用例数"))
            .render("./caseReportManage/templates/caseReportManage/e_chart_2.html")
    )
    project = Project.objects.all()
    p = request.GET.get('current_page', 1)
    paginator = Paginator(project, 3)
    p = int(p)
    page_range = transfer_the_page_range(p, paginator)
    if paginator.num_pages - p > 5:
        last_page = True
    try:
        pages = paginator.page(p)
    except PageNotAnInteger:
        pages = paginator.page(1)
    except EmptyPage:
        pages = paginator.page(paginator.num_pages)
    return render(request, 'caseReportManage/index.html', locals())


def echart_1(request):
    return render(request, 'caseReportManage/e_chart_1.html')


def echart_2(request):
    return render(request, 'caseReportManage/e_chart_2.html')


def edit_report(request):
    return HttpResponse("Not Yet!!!")